//
//  RootTableViewController.m
//  GXW
//
//  Created by yd on 16/10/24.
//  Copyright © 2016年 yd. All rights reserved.
//

#import "RootTableViewController.h"
#define cellid @"cellid"
@interface RootTableViewController ()

@end

@implementation RootTableViewController

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self setNormol];
    [self setUpNav];
    [self setUpTableView];
    
    
}
- (void)setNormol{
    
       
        self.view.backgroundColor = BackGround_Color;
        self.page = 1;
 
}
- (void)setUpNav{
    self.nav = [[UIView alloc]init];
    self.nav.backgroundColor = MAIN_Color;
    [self.view addSubview:self.nav];
    [self.nav mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(64);
    }];
    self.myTitle = [[UILabel alloc]init];
    self.myTitle.textColor = Bai_Color;
    self.myTitle.textAlignment = 1;
    self.myTitle.font = [UIFont systemFontOfSize:20];
    [self.nav addSubview:self.myTitle];
    [self.myTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.centerX.mas_equalTo(0);
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(44);
    }];
    
    UIView *line = [[UIView alloc]init];
    line.backgroundColor = Line_Color;
    [self.nav addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(1);
    }];
}
-(void)setTheBackBtn{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:nil forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"return"] forState:UIControlStateNormal];
    [button setImageEdgeInsets:UIEdgeInsetsMake(3, 10, 0, 0)];
    [button addTarget:self action:@selector(leftButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.nav addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.left.mas_equalTo(0*WidthRate);
        make.width.mas_equalTo(44);
        make.height.mas_equalTo(44);
    }];
}

-(void)setLeftImg:(UIImage *)leftImg{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:nil forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setImage:leftImg forState:UIControlStateNormal];
    [button setImageEdgeInsets:UIEdgeInsetsMake(3, 10, 0, 0)];
    [button addTarget:self action:@selector(leftButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.nav addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.left.mas_equalTo(15*WidthRate);
        make.width.mas_equalTo(44);
        make.height.mas_equalTo(44);
    }];
}


-(void)leftButtonClick{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)setRightBtnTitle:(NSString *)rightBtnTitle{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setExclusiveTouch:YES];
    [button setTitle:rightBtnTitle forState:UIControlStateNormal];
    [button setTitleColor:Bai_Color forState:UIControlStateNormal];
    [button setContentHorizontalAlignment:2];
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    [button addTarget:self action:@selector(rightButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.nav addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.right.mas_equalTo(-15*WidthRate);
        make.width.mas_equalTo(44);
        make.height.mas_equalTo(44);
    }];
}

-(void)setRightBtnImg:(UIImage *)rightBtnImg{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setExclusiveTouch:YES];
    [button setImage:rightBtnImg forState:UIControlStateNormal];
    [button addTarget:self action:@selector(rightButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.nav addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.right.mas_equalTo(-10);
        make.width.mas_equalTo(44);
        make.height.mas_equalTo(44);
    }];
}

-(void)rightButtonClick{
    
}


- (void)setUpTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableView.backgroundColor = BackGround_Color;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellid];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(64);
        make.bottom.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
    }];
    
    self.noDataView = [[NoDataView alloc]initWithFrame:CGRectZero];
    self.noDataView.hidden = YES;
    [self.tableView addSubview:self.noDataView];
    [self.noDataView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
    }];
    [self.noDataView.view mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.tableView.mas_centerX).offset(0);
        make.centerY.equalTo(self.tableView.mas_centerY).offset(-60);
        make.size.mas_equalTo(CGSizeMake(100, 100));
    }];
    [self.noDataView.label mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.tableView.mas_centerX).offset(0);
        make.centerY.equalTo(self.tableView.mas_centerY).offset(10);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        if (IsPad) {
            make.height.mas_equalTo(18*PadHeightRate);
        }else{
            make.height.mas_equalTo(18*HeightRate);
        }
    }];
}
- (void)resetTableViewFrame:(CGRect)frame{
    self.tableView.frame = frame;
    self.noDataView.frame = CGRectMake(0, 0, self.tableView.frame.size.width, self.tableView.frame.size.height);
}
- (void)setWebData{
   
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellid];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50*WidthRate;
}
- (NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc]initWithCapacity:1];
    }
    return _dataArray;
}

- (void)setRefreshHeaderFooterView{
   
    DefineWeakSelf;
  MJRefreshBackNormalFooter *freshFoot = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
      weakSelf.page = weakSelf.page + 1;
      [weakSelf setWebData];
  }];
    self.tableView.mj_footer = freshFoot;
    

}

- (void)setPullLoadMoreEnable:(BOOL)pullLoadMoreEnable{
    if (pullLoadMoreEnable) {
        if (self.tableView.mj_footer == nil) {
            [self setRefreshHeaderFooterView];
        }
    }else{
        self.tableView.mj_footer = nil;
        UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
        view.backgroundColor = [UIColor clearColor];
        self.tableView.tableFooterView = view;
    }
}

- (void)setPullRefreshEnable:(BOOL)pullRefreshEnable{
    
    
    if (pullRefreshEnable) {
        DefineWeakSelf;
        // 设置回调（一旦进入刷新状态就会调用这个refreshingBlock）
        if (self.tableView.mj_header == nil) {
            MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
                weakSelf.page = 1;
                [weakSelf setWebData];
            }];
            self.tableView.mj_header = header;
            
        }
    }else{
        if (self.tableView.mj_header != nil) {
            self.tableView.mj_header = nil;
            
        }
    }
}
- (void)MJRefreshStop{
    [self.tableView.mj_header  endRefreshing];
    [self.tableView.mj_footer  endRefreshing];
}


@end
